// mircoApp/cultivateCourse/manage/recording/recording.js
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
const app = getApp();

Page({
  behaviors: [skinBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    total: '',
    paymentPrice: '',
    // 选择会员
    show: false,
    result_user: '',
    showType: "",
    memberList: [],
    goodsList: [],
    courseList: [],
    member_id: {},
    goods_id: {},
    course_term_id: {},
    proof_imgs: [],
    group_name: '',
    group_sn: '',
    searchVal: '',
    radio: '0',

    total_page: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    if (options.id) {
      console.log('有id的是重新提交')
      this.getOrder();
    }
  },
  init() {
    this.setData({
      show: false,
      page: 1,
      total_page: 0,
      memberList: [],
      goodsList: [],
      courseList: [],
    })
  },
  onSearch() {
    console.log(this.data.searchVal)
    this.setData({
      page: 1,
      total_page: 0,
      memberList: [],
      goodsList: [],
      courseList: [],
    })
    this.getSelectData();
  },
  onChange(e) {
    this.setData({
      radio: e.detail
    })
  },
  tapSelect(e) {
    let {
      ind,
      name
    } = e.currentTarget.dataset;
    if (this.data.showType == 'good') {
      this.setData({
        goods_id: {
          id: ind,
          name
        }
      })
    } else if (this.data.showType == 'user') {
      this.setData({
        member_id: {
          id: ind,
          name,
          parent: e.currentTarget.dataset?.parent && e.currentTarget.dataset?.parent?.nickname ? e.currentTarget.dataset?.parent.nickname : '暂无'
        }
      })
    } else if (this.data.showType == 'date') {
      this.setData({
        course_term_id: {
          id: ind,
          name
        }
      })
    } else if (this.data.showType == 'bank') {
      this.setData({
        bank: name
      })
      console.log(this.data.bank, 'ddddddddddd')
    }
    this.setData({
      show: false,
      page: 1,
      total_page: 0
    })
  },
  checkboxChange(e) {
    console.log(e)
    this.setData({
      result: e.detail
    })
  },
  onClose() {
    this.setData({
      show: false,
      showType: '',
      page: 1,
      total_page: 0,
      memberList: [],
      goodsList: [],
      courseList: [],
      searchVal: ''
    })
  },
  async selectUser(e) {
    let {
      taptype
    } = e.currentTarget.dataset;
    this.setData({
      showType: taptype,
      page: 1,
      total_page: 0,
      memberList: [],
      goodsList: [],
      courseList: [],
      searchVal: ''
    })
    if (taptype != 'user') {
      await this.getSelectData();
    }
    this.setData({
      show: true
    })
  },
  //选择图片
  onRead() {
    let that = this;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFiles;
        let photourl = tempFilePaths[0].tempFilePath;
        console.log(res.tempFiles, photourl)
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: 'file',
          success(resdata) {
            console.log(resdata)
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                proof_imgs: res.data.img_url,
              });
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: (err) => {
            console.log(err)
          }
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let basic_info = wx.getStorageSync('yz_basic_info');
    wx.setNavigationBarTitle({
      title: basic_info.plugin_setting.cultivate_course.cultivate_live_name,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit() {
    let json = {
      form: {}
    };
    json.form.member_id = this.data.member_id.id;
    json.form.goods_id = this.data.goods_id.id;
    json.form.total = this.data.total;
    json.form.price = this.data.paymentPrice;
    
    json.form.proof_imgs = [this.data.proof_imgs];
    json.form.bank = this.data.bank;
    json.form.order_type = this.data.radio;
    if(this.data.radio != 1){
      json.form.course_term_id = this.data.course_term_id.id;
      json.form.group_sn = this.data.group_sn;
      json.form.group_name = this.data.group_name;
    }
    if (this.options.id) {
      json.id = this.options.id
    }
    console.log(json)
    if (app._isTextEmpty(this.data.member_id.id)) {
      app.tips('选择购买会员');
      return
    }
    if (app._isTextEmpty(this.data.goods_id.id)) {
      app.tips('选择课程商品');
      return
    }
    if (app._isTextEmpty(this.data.total)) {
      app.tips('请输入购买数量');
      return
    }
    if (app._isTextEmpty(this.data.paymentPrice)) {
      app.tips('请输入实付金额');
      return
    }
    if(this.data.radio != 1){
      if (app._isTextEmpty(this.data.course_term_id.id)) {
        app.tips('请选择现场成交课期');
        return
      }
      if (app._isTextEmpty(this.data.course_term_id.id)) {
        app.tips('请选择现场成交课期');
        return
      }
      if (app._isTextEmpty(this.data.group_name)) {
        app.tips('请输入桌长姓名');
        return
      }
      if (app._isTextEmpty(this.data.group_sn)) {
        app.tips('请输入桌号');
        return
      }
    }
    if (app._isTextEmpty(this.data.proof_imgs)) {
      app.tips('请上传图片');
      return
    }
    if (app._isTextEmpty(this.data.bank)) {
      app.tips('请选择收款进账银行');
      return
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.submit"),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips('提交成功');
          wx.navigateBack({
            delta: 1
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getOrder() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.get-live-order"),
      data: {
        id: this.options.id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data)
          let { member, price, course_term, goods, group_name, group_sn, parent_member, proof_imgs, order_type, total, bank } = res.data;
          this.data.member_id.name = member.nickname;
          this.data.member_id.id = member.uid;
          this.data.goods_id.id = goods.id;
          this.data.goods_id.name = goods.title;
          this.data.total = total;
          this.data.paymentPrice = price;
          if(course_term){
            this.data.course_term_id.name = course_term.name;
            this.data.course_term_id.id = course_term.id;
          }
          this.data.group_sn = group_sn;
          this.data.group_name = group_name;
          this.data.member_id.parent = parent_member ? parent_member.nickname : '暂无';
          this.data.bank = bank;
          this.data.radio = order_type;
          this.setData({
            member_id: this.data.member_id,
            goods_id: this.data.goods_id,
            total: this.data.total,
            paymentPrice: this.data.paymentPrice,
            course_term_id: this.data.course_term_id,
            group_sn: group_sn,
            group_name: group_name,
            proof_imgs: proof_imgs[0],
            bank: bank,
            radio: String(order_type)
          })
          console.log(this.data.proof_imgs)
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    this.getSelectData('More')
  },
  getSelectData(key = '') {
    let urlStr = "";
    let json = {
      keyword: this.data.searchVal,
      page: this.data.page
    };
    if (this.data.showType == 'user') {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.get-member-list");
    } else if (this.data.showType == 'good') {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.get-goods-list");
    } else if (this.data.showType == 'date') {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.get-course-term");
    } else if (this.data.showType == 'bank') {
      urlStr = app.getNetAddresss("plugin.cultivate-course.frontend.controllers.course.live-order.get-bank-list")
    }
    if (key == 'More') {
      // 加载更多
      if (this.data.page >= this.data.total_page) {
        return;
      } else {
        this.setData({
          page: this.data.page + 1
        })
        json.page = this.data.page;
      }
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (this.data.showType == 'user') {
            if (key == 'More') {
              this.setData({
                memberList: this.data.memberList.concat(res.data.data)
              })
              return
            }
            this.setData({
              memberList: res?.data?.data || []
            })
          } else if (this.data.showType == 'good') {
            if (key == 'More') {
              this.setData({
                goodsList: this.data.goodsList.concat(res.data.data)
              })
              return
            }
            this.setData({
              goodsList: res?.data?.data || []
            })
          } else if (this.data.showType == 'date') {
            if (key == 'More') {
              this.setData({
                courseList: this.data.courseList.concat(res.data.data)
              })
              return
            }
            this.setData({
              courseList: res?.data?.data || []
            })
          } else if (this.data.showType == 'bank') {
            if (key == 'More') {
              this.setData({
                bankList: this.data.bankList.concat(res.data)
              })
              return
            }
            this.setData({
              bankList: res.data
            })
          }
          if (key != 'More') {
            this.data.total_page = res.data.last_page;
            if (!this.data.total_page) {
              this.data.total_page = 0;
            }
            this.setData({
              total_page: this.data.total_page,
            })
          }
        } else {
          this.data.isLoadMore = false;
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
})